package com.daoeping.dao;

import java.util.List;
import org.json.JSONArray;
import org.springframework.transaction.annotation.Transactional;

import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.util.ConverntJsonData;

public class AllDaoSupport extends  DataBaseDaoImpl{
	@Transactional
	public Object getPageList(Page page, String hql) {
		
		int temp = (page.getPage() - 1) * page.getPageSize();
		return pageDataByHQL(hql , temp ,page.getPageSize() );
		
	}
	
	@Transactional
	public int getNum(Page page, String hql)throws Exception{
		
		List<?> list = (List<?>) selectDataByHQL(hql);
		int rowNum = 0;
		if (list.size() != 0) {
			rowNum = Integer.parseInt(list.get(0).toString());
		}
		return rowNum;
	}
	
	@Transactional
	public JsonData pageData(Page page, String hql , JsonData jsonData) throws Exception {
		List<?> resultlist = (List<?>) getPageList(page, hql);
		JSONArray json = new ConverntJsonData().getJsonData(resultlist);  
		JSONArray newJson = new JSONArray();
		newJson.put(getNum(page, hql));
		for (int i = 0; i < json.length(); i++) {
			newJson.put(json.get(i));
			}
		System.out.println(newJson.toString());
		jsonData.setJsonData(newJson.toString());
		return jsonData;
	}

}
